const WebSocket = require('ws');
const https = require('https')
const fs = require('fs');
const path = require('path')
const httpOptions={
    key:fs.readFileSync(path.join(__dirname,'./czy.mathobj.xyz.key')),
    cert:fs.readFileSync(path.join(__dirname,'./czy.mathobj.xyz_bundle.pem'))
}
const httpServer = https.createServer(httpOptions).listen(3333,()=>{
    console.log('my app is running')
})
//创建服务
const wss = new WebSocket.Server({ server:httpServer });

//当用户连接的时候回触发connection
wss.on('connection', (client) => {
   //接收客户端消息
   client.on('message', (data) => {
       console.log(data.toString())
       client.send(data.toString())
       //遍历所有用户,将接收到的消息发送给所有的用户
    //    wss.clients.forEach((item) => {
    //        if (item.readyState === WebSocket.OPEN) {
    //            item.send(data);
    //       }
    //   });
  });
});
